import { Injectable } from '@nestjs/common';
import axios from 'axios';
import * as cheerio from 'cheerio';
const xlsx = require('node-xlsx');
const fs = require('fs');

@Injectable()
export class ProductService {

  async getProduct() {
    const url = `http://quotes.money.163.com/f10/zycwzb_600519.html#01c01`;
    const res = await axios.get(url);
    const $ = cheerio.load(res.data);
    let tdStr = $('.col_r .table_bg001  tbody tr:nth-child(12)').html()
    let reg = /<td[\S\s]*?>([\S\s]*?)<\/td>/gi;
    let result = [];
    let a
    while ((a = reg.exec(tdStr)) != null) {
      result.push(a[1].replace('\n', ''));
    }
    let datas = []
    let title = ['净利润(万元)']

    datas = [title, result]
    var buffer = xlsx.build([{ name: 'sheet1', data: datas }]);
    fs.writeFileSync(title[0] + '.xlsx', buffer, { 'flag': 'w' });
    return;
  }
}
